//: src/com/silvtar/net/NetPack.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 4/21/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 4/26/2013   0.1        Create the class
 * 5/20/2013   0.2        Add function to close serversocket 
 */
package com.silvtar.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.silvtar.io.StdOut;

/**
 * 包装类。包装各种会发送异常的函数。
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class NetPack {

    // ============================================================
    // 服务器相关
    // ============================================================

    /**
     * 绑定端口号
     * 
     * @param port
     *            端口号
     * @return ServerSocket对象
     */
    public static ServerSocket serverBind(int port) {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            StdOut.printErr("Server bind port " + port + " fail!");
            e.printStackTrace();
        }
        return serverSocket;
    }

    /**
     * 监听ServerSocket
     * 
     * @param serverSocket
     *            要监听的ServerSocket
     * @return 与客户端连接的socket
     */
    public static Socket serverAccept(ServerSocket serverSocket) {

        Socket socket = null;
        try {
            socket = serverSocket.accept();
        } catch (SocketException e) {
            return socket;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }

    /**
     * 关闭ServerSocket
     * 
     * @param serverSocket
     *            要关闭的ServerSocket
     */
    public static void serverClose(ServerSocket serverSocket) {

        try {
            serverSocket.close();
        } catch (IOException e) {
            StdOut.printErr("Server closeing the socket on port "
                    + serverSocket.getLocalPort() + " fail!");
            e.printStackTrace();
        }
    }

    // ============================================================
    // 客户端相关
    // ============================================================

    /**
     * 与服务器建立连接。
     * 
     * @param netAddr_S
     *            要连接的服务器的网络地址或主机名
     * @param port
     *            要连接的服务器的端口号
     * @return 与服务器连接的Socket
     */
    public static Socket connect(String netAddr_S, int port) {

        InetAddress netAddr_I = null;
        Socket socket = null;
        try {
            netAddr_I = InetAddress.getByName(netAddr_S);
            socket = new Socket(netAddr_I, port);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }

        return socket;
    }

    /**
     * 与服务器建立连接。
     * 
     * @param netAddr_I
     *            要连接的服务器的网络地址或主机名
     * @param port
     *            要连接的服务器的端口号
     * @return 与服务器连接的Socket
     */
    public static Socket connect(InetAddress netAddr_I, int port) {

        Socket socket = null;
        try {
            socket = new Socket(netAddr_I, port);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }

        return socket;
    }
}
